package utils;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import model.Response;
import dtos.ResponseDTO;

/**
 * 
 * @author Radulescu Cristina
 * @author Pop Loredana
 *
 */
@Stateless
@LocalBean
public class ResponseDAO implements ResponseDAORemote {

	@PersistenceContext
	EntityManager em;

	@EJB
	Conv conv;

	/**
	 * 
	 * Default constructor.
	 */

	public ResponseDAO() {

		// TODO Auto-generated constructor stub

	}

	/**
	 * Method used to insert response
	 * @param Response
	 */
	@Override
	public void insertResponse(ResponseDTO rd) {

		Response r1 = em.find(Response.class, rd.getId());
		if (r1 == null) {
			Response r = conv.fromEntityResponseDTO(rd);
			em.persist(r);
		} else {
			em.merge(r1);
		}

	}

	/**
	 * Method used to get response by id
	 * @param id
	 * @return response
	 */
	@Override
	public ResponseDTO getResponse(int id) {
		return conv.fromEntityResponse(em.find(Response.class, id));
	}

	/**
	 * Method used to update Response
	 * @param ud Response
	 */
	@Override
	public void updateResponse(ResponseDTO ud) {
		if (em.find(Response.class, ud.getId()) != null)
			em.merge(conv.fromEntityResponseDTO(ud));
	}

	/**
	 * Method used to get a list of responses by id
	 * @return list of responses
	 */
	@Override
	public List<ResponseDTO> getAllResponsesByUserId(int id) {
		List<ResponseDTO> resultlist = new ArrayList<>();
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Response> cq = cb.createQuery(Response.class);
		Root<Response> resp = cq.from(Response.class);
		cq.select(resp);

		TypedQuery<Response> q = em.createQuery(cq);

		List<Response> responses = q.getResultList();

		for (Response d : responses) {
			if (d.getUser() != null)
				if (d.getUser().getId() == id)
					resultlist.add(conv.fromEntityResponse(d));
		}

		return resultlist;
	}

	/**
	 * Method used to delete response
	 * @param ud of type response
	 */
	@Override
	public void deleteResponse(ResponseDTO ud) {
		Response r = em.find(Response.class, ud.getId());
		em.createQuery("delete from Response where id=:id")
				.setParameter("id", r.getId()).executeUpdate();
	}

	/**
	 * Method used to get a list of reponses
	 * @return list of responses
	 */
	@Override
	public List<ResponseDTO> getAllResponses() {
		List<ResponseDTO> resultlist = new ArrayList<>();
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Response> cq = cb.createQuery(Response.class);
		Root<Response> resp = cq.from(Response.class);
		cq.select(resp);

		TypedQuery<Response> q = em.createQuery(cq);

		List<Response> responses = q.getResultList();

		for (Response d : responses) {
			resultlist.add(conv.fromEntityResponse(d));
		}
		return resultlist;
	}
}
